package com.example.szh.mvp.model;

import com.example.szh.Api;
import com.example.szh.entity.BaseEntity;
import com.example.szh.entity.ChannelEntity;
import com.example.szh.entity.CommentEntity;
import com.example.szh.entity.FollowEntity;
import com.example.szh.entity.ImageBannerEntity;
import com.example.szh.entity.LiveCommentEntity;
import com.example.szh.entity.LiveEntity;
import com.example.szh.entity.LocalChannelEntity;
import com.example.szh.entity.MoreChannelEntity;
import com.example.szh.entity.NewsEntity;
import com.example.szh.entity.RecommendEntity;
import com.example.szh.entity.TextBannerEntity;
import com.example.szh.entity.UserFollowEntity;
import com.example.szh.entity.VideoNewsEntity;
import com.example.szh.http.HttpCode;
import com.example.szh.http.HttpFactory;
import com.example.szh.http.HttpType;
import com.example.szh.mvp.contract.LREContract;
import com.example.szh.nextwork.ChangeFunction;
import com.example.szh.nextwork.model.BaseModel;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.Observable;

public class LREModel extends BaseModel implements LREContract.ILREModel {
    @Inject
    public LREModel(){}
    @Override
    public Observable<BaseEntity> requestAll(Map<String, Object>... maps) {
        //根据传递进来的maps 数量创建生成专门用来存放被观察者的集合
        List<Observable<BaseEntity>> observables=new ArrayList<>();
        for (int i = 0; i < maps.length; i++) {
            Map<String,Object> map=maps[i];
            observables.add(change2BaseEntity(map));
        }
        return Observable.merge(observables);
    }

    //异步任务转化为BaseEntity任务
    private Observable<BaseEntity> change2BaseEntity(Map<String,Object> map){
        int code = (int) map.get("code");
        map.remove("code");
        Observable<BaseEntity> ob = null;
        switch (code){
            case HttpCode.USERCHANNELCODE:
                //获取首页用户频道接口
                ob =  HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestUserChannels(createBody(map))
                        .map(new ChangeFunction<ChannelEntity>());
                break;
            case HttpCode.CHANNELNEWS:
                //按频道获取新闻列表接口
                ob =  HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestNews(createBody(map))
                        .map(new ChangeFunction<NewsEntity>());
                break;
            case HttpCode.IMAGERBANNERCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKEN)
                        .getRetrofit().create(Api.class)
                        .requestImageBanner().map(new ChangeFunction<ImageBannerEntity>());
                break;
            case HttpCode.TEXTBANNERCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKEN)
                        .getRetrofit().create(Api.class)
                        .requestTextBanner().map(new ChangeFunction<TextBannerEntity>());
                break;
            case HttpCode.RECOMMENDNEWSCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKEN)
                        .getRetrofit().create(Api.class)
                        .requestRecommendNews()
                        .map(new ChangeFunction<RecommendEntity>());
                break;
            case HttpCode.FOLLOWNEWSCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestFollowNews(createBody(map))
                        .map(new ChangeFunction<FollowEntity>());
                break;
            case HttpCode.USERFOLLOWCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestUserFollow(createBody(map))
                        .map(new ChangeFunction<UserFollowEntity>());
                break;
                case HttpCode.VIDEONEWSCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                            .getRetrofit().create(Api.class)
                            .requestVideoNews(createBody(map))
                            .map(new ChangeFunction<VideoNewsEntity>());
                    break;
                case HttpCode.LIVESCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                            .getRetrofit().create(Api.class)
                            .requestLive(createBody(map))
                            .map(new ChangeFunction<LiveEntity>());
                    break;
                case HttpCode.SELCOMMENTCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                            .getRetrofit().create(Api.class)
                            .requestSelComment(createBody(map))
                            .map(new ChangeFunction<CommentEntity>());
                    break;
                case HttpCode.ADDCOMMENTCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                            .getRetrofit().create(Api.class)
                            .requestAddComment(createBody(map))
                            .map(new ChangeFunction<BaseEntity>());
                    break;
                case HttpCode.ADDLIVECOMMENTCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKEN)
                            .getRetrofit().create(Api.class)
                            .requestAddLiveComment(createBody(map))
                            .map(new ChangeFunction<BaseEntity>());
                    break;
                case HttpCode.SELLIVECOMMENTCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                            .getRetrofit().create(Api.class)
                            .requestSelLiveComment(createBody(map))
                            .map(new ChangeFunction<LiveCommentEntity>());
                    break;
                case HttpCode.ADDCollectNewsCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                            .getRetrofit().create(Api.class)
                            .requestAddCollectNews(createBody(map))
                            .map(new ChangeFunction<BaseEntity>());
                    break;
                case HttpCode.DELCollectNewsCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                            .getRetrofit().create(Api.class)
                            .requestDelCollectNews(createBody(map))
                            .map(new ChangeFunction<BaseEntity>());
                    break;
                case HttpCode.ISCollectNewsCODE:
                    ob=HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                            .getRetrofit().create(Api.class)
                            .requestIsCollectNews(createBody(map))
                            .map(new ChangeFunction<BaseEntity>());
                    break;
            case HttpCode.LOCALCHANNELCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestLocalChannels(createBody(map))
                        .map(new ChangeFunction<LocalChannelEntity>());
                break;
            case HttpCode.MORECHANNELCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKENSIGNTYPE)
                        .getRetrofit().create(Api.class)
                        .requestMoreChannels(createBody(map))
                        .map(new ChangeFunction<MoreChannelEntity>());
                break;
            case HttpCode.COLLECTIONCHANNELCODE:
                ob = HttpFactory.getInstance().factory(HttpType.TOKEN)
                        .getRetrofit().create(Api.class)
                        .requestCollectionChannels(createBody(map));
                break;
        }
        return ob;
    }

    @Override
    public Observable<BaseEntity> request_refresh_load(Map<String, Object> map) {
        return change2BaseEntity(map);
    }
}
